<template>
  <div class="export-container">
    <div class="export-demo-box">
      <div>已选择：{{ selectedItems.length }}条数据</div>
      <div class="export-demo-check">
        <div>导出字段：</div>
        <el-checkbox v-model="checkAll" :indeterminate="isIndeterminate" @change="handleCheckAllChange">
          全选
        </el-checkbox>
      </div>
    </div>
    <div class="export-demo-checkbox">
      <el-checkbox-group v-model="checkedItem" @change="handleCheckedCitiesChange">
        <el-checkbox v-for="city in cities" :key="city" :label="city" :value="city">
          {{ city }}
        </el-checkbox>
      </el-checkbox-group>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, defineProps, defineEmits } from 'vue';

const props = defineProps<{
  cities: string[];
  selectedItems: any[]; // 表格选中的数据
}>();

const emits = defineEmits<{
  (event: 'update:checkedItem', value: string[]): void;
}>();

const checkAll = ref(false);
const isIndeterminate = ref(true);
const checkedItem = ref<string[]>([]);

// 全选
const handleCheckAllChange = (val: boolean) => {
  checkedItem.value = val ? props.cities : [];
  isIndeterminate.value = false;
  emits('update:checkedItem', checkedItem.value);
};

const handleCheckedCitiesChange = (value: string[]) => {
  const checkedCount = value.length;
  checkAll.value = checkedCount === props.cities.length;
  isIndeterminate.value = checkedCount > 0 && checkedCount < props.cities.length;
  emits('update:checkedItem', checkedItem.value);
};
</script>

<style scoped lang="scss">
.export-container {
  width: 100%;
  height: 100%;
  padding: 0 2%;
  box-sizing: border-box;

  .export-demo-check {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .export-demo-checkbox {
    border: 1px solid #dcdfe6;
    padding: 0.5rem;
  }

  .el-checkbox {
    width: 6rem;
    display: inline-table;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}
</style>
